package com.gt.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.app.common.DietWayEnum;
import com.gt.app.domain.EsDietWayInfo;
import com.gt.app.domain.EsPigOilType;
import com.gt.app.domain.dto.EsDietWayInfoDto;
import com.gt.app.mapper.EsDietWayInfoMapper;
import com.gt.app.service.EsDietWayInfoService;
import com.gt.app.service.EsPigOilTypeService;
import com.gt.app.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 饮食方式信息表 服务实现类
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@Service
public class EsDietWayInfoServiceImpl extends ServiceImpl<EsDietWayInfoMapper, EsDietWayInfo> implements EsDietWayInfoService {
    @Autowired
    private EsPigOilTypeService esPigOilTypeService;

    @Override
    @Transactional
    public void add(EsDietWayInfoDto esDietWayInfoDto) {
        if (esDietWayInfoDto == null){
            return;
        }
        /**如果信息存在，先删除再保存**/
        LambdaQueryWrapper<EsDietWayInfo> eq = new LambdaQueryWrapper<EsDietWayInfo>().eq(EsDietWayInfo::getEsCheckId, esDietWayInfoDto.getEsCheckId());
        EsDietWayInfo dietWayInfo = this.getOne(eq);
        if (dietWayInfo != null) {
            this.remove(eq);
            LambdaQueryWrapper<EsPigOilType> wrapper = new LambdaQueryWrapper<EsPigOilType>()
                    .eq(EsPigOilType::getLifeOrdrowid, dietWayInfo.getOrdrowid());
            esPigOilTypeService.remove(wrapper);
        }
        /**开始保存数据**/
        EsDietWayInfo esDietWayInfo = new EsDietWayInfo();
        BeanUtils.copyProperties(esDietWayInfoDto, esDietWayInfo);
        this.save(esDietWayInfo);

        if (CommonUtils.isNotBlank(esDietWayInfoDto.getPigMakeType())) {
            ArrayList<EsPigOilType> esPigOilTypes = new ArrayList<>();
            for (String pigMakeType : esDietWayInfoDto.getPigMakeType()) {
                EsPigOilType esPigOilType = new EsPigOilType();
                esPigOilType.setLifeOrdrowid(esDietWayInfo.getOrdrowid());
                esPigOilType.setType(DietWayEnum.PIG_MAKE_WAY.getName());
                esPigOilType.setItem(pigMakeType);
                esPigOilTypes.add(esPigOilType);
            }
            esPigOilTypeService.saveBatch(esPigOilTypes);
        }

        if (CommonUtils.isNotBlank(esDietWayInfoDto.getAnimalOil())) {
            ArrayList<EsPigOilType> esPigOilTypes = new ArrayList<>();
            for (String pigMakeType : esDietWayInfoDto.getAnimalOil()) {
                EsPigOilType esPigOilType = new EsPigOilType();
                esPigOilType.setLifeOrdrowid(esDietWayInfo.getOrdrowid());
                esPigOilType.setType(DietWayEnum.ANIMAL_OIL.getName());
                esPigOilType.setItem(pigMakeType);
                esPigOilTypes.add(esPigOilType);
            }
            esPigOilTypeService.saveBatch(esPigOilTypes);
        }

        if (CommonUtils.isNotBlank(esDietWayInfoDto.getPlantOil())) {
            ArrayList<EsPigOilType> esPigOilTypes = new ArrayList<>();
            for (String pigMakeType : esDietWayInfoDto.getPlantOil()) {
                EsPigOilType esPigOilType = new EsPigOilType();
                esPigOilType.setLifeOrdrowid(esDietWayInfo.getOrdrowid());
                esPigOilType.setType(DietWayEnum.PLANT_OIL.getName());
                esPigOilType.setItem(pigMakeType);
                esPigOilTypes.add(esPigOilType);
            }
            esPigOilTypeService.saveBatch(esPigOilTypes);
        }

    }

    @Override
    public EsDietWayInfoDto getDietWayInfoById(String checkId) {
        EsDietWayInfoDto esDietWayInfoDto = new EsDietWayInfoDto();

        LambdaQueryWrapper<EsDietWayInfo> eq = new LambdaQueryWrapper<EsDietWayInfo>().eq(EsDietWayInfo::getEsCheckId, checkId);
        EsDietWayInfo esDietWayInfo = this.getOne(eq);
        if (esDietWayInfo == null){
            return null;
        }
        BeanUtils.copyProperties(esDietWayInfo, esDietWayInfoDto);

        LambdaQueryWrapper<EsPigOilType> wapper = new LambdaQueryWrapper<EsPigOilType>().eq(EsPigOilType::getLifeOrdrowid, esDietWayInfo.getOrdrowid());
        List<EsPigOilType> list = esPigOilTypeService.list(wapper);

        List<String> pigMakeType = new ArrayList<String>();
        List<String> animalOil = new ArrayList<String>();
        List<String> plantOil = new ArrayList<String>();


        for (EsPigOilType esPigOilType : list) {
            if (DietWayEnum.PIG_MAKE_WAY.getName().equals(esPigOilType.getType())) {
                pigMakeType.add(esPigOilType.getItem());
            }

            if (DietWayEnum.ANIMAL_OIL.getName().equals(esPigOilType.getType())) {
                animalOil.add(esPigOilType.getItem());
            }

            if (DietWayEnum.PLANT_OIL.getName().equals(esPigOilType.getType())) {
                plantOil.add(esPigOilType.getItem());
            }
        }
        esDietWayInfoDto.setPigMakeType(pigMakeType);
        esDietWayInfoDto.setAnimalOil(animalOil);
        esDietWayInfoDto.setPlantOil(plantOil);
        return esDietWayInfoDto;
    }
}
